package cn.gz.demo.adapter;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;

public class ListViewAdapter extends BaseAdapter {
	private Activity context;
	private List<String> list;
	private LayoutInflater inflater;

	public ListViewAdapter(Activity context, ArrayList<String> list) {
		this.context = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list == null ? 0:list.size();
	}

	@Override
	public String getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	static class ViewHolder{
		TextView tv;
	}
	
	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder holder;
		if(arg1 == null){
			holder = new ViewHolder();
			arg1 = inflater.inflate(R.layout.list_item, null);
			holder.tv = (TextView) arg1.findViewById(R.id.item_tv);
			arg1.setTag(holder);
		}else{
			holder = (ViewHolder) arg1.getTag();
		}
		
		holder.tv.setText(getItem(arg0));
		return arg1;
	}

}
